// view/index/addyuanliao/addyuanliao.js
let app = getApp()
let yuming = app.globalData.yuming
let reg = app.globalData.mobile
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    buttonClicked: false,
    yuming: yuming,
    heights: "",
    title: '添加供应商',
    spmclist: [{
      name: '工作服ABO'
    }, {
      name: '山东芹菜'
    }, {
      name: '大蒜'
    }, {
      name: '西红柿'
    }],
    spmcindex: 0,
    jinhuonumlist: [{
      name: 12
    }, {
      name: 23
    }], //进货数量
    jinhuonumindex: 0, //进货数量
    numdanindex: 0, //数量单位
    numdanlist: [{
      name: '个'
    }, {
      name: '箱'
    }, {
      name: '斤'
    }], //数量单位
    fazlist: [{
      name: '雨林区发证机关'
    }], //  供应商
    fazindex: 0, // 供应商
    xukejylblist: [{
      name: '食品'
    }], //原料类型
    xukejylbindex: 0, //原料类型
    date: '请选择', //开始
    dates: '请选择', //结束
    gongguo: '请选择', //供应商
    yltypeguo: '请选择经营类型', //原料类型
    photo: [],
    wuphoto: '',
    zhengs: [{
        tit: '营业执照',
        state: 0,
        img: '{{yuming}}static/applet/images/index/pho.png',
        tishi: '上传最新的营业执照正本或副本'
      },
      {
        tit: '其他证照',
        state: 1,
        img: '{{yuming}}static/applet/images/index/pho.png',
        tishi: '餐饮行业或其他行业需上传行业许可证'
      },
      {
        tit: '店头照',
        img: '',
        tishi: '上传与营业执照 名称相符的店头照'
      },
      {
        tit: '您的免冠照',
        img: '',
        tishi: '法人或负责人正面照'
      },
    ],
    shul: 1,
    lie: [{
      materialName: '',
      materialNums: '',
      materialUnit: ''
    }],
    dianname: '', //店铺名字
    xinyong: '', //信用代码
    fapename: '', //法人姓名
    jylxlist: ['蔬菜', '水果'], //经营类型
    jylxindex: 0,
    jylxguo: '请选择经营类型',
    xiangadress: '', //详细地址
    jydianhua: '', //经营电话
    yyzz: '', //营业执照
    otherzz: '', //其他证照
    diantz: '', //店头照
    miangz: '', //免冠照
    wudiantz: '', //不带域名店头照
    wuyyzz: '', //不带域名营业执照
    wuotherzz: '', //不带域其他证照
    wumiangz: '', //不带域名免冠照
    jylxid: '', //经营类型id
    sldwguo: '请选择',
    addid: '',
    titileAdd:''
  },
  //营业执照一键识别
  yjsbyyz(imgurl) {
    wx.showLoading({
      title: '识别营业执照中',
      icon: 'none'
    })
    let that = this
    this.myRequest(yuming + 'Besall/ai_business_license_v1', {
      imgUrl: imgurl
    }, "POST").then(res => {
      if (res.data.code == 200) {
        wx.showToast({
          title: '识别成功',
          icon: 'none'
        })
        wx.hideLoading();
        let data = res.data.data.营业执照识别实体信息
        that.setData({
          dianname: data.企业名称,
          xinyong: data.企业统一社会信用编码,
          xiangadress: data.企业注册地址,
        })
      }
    })
  },
  showModal(e) {
    var aaa = e.currentTarget.dataset.aaa
    var img = e.currentTarget.dataset.img
    this.setData({
      fdaaa: aaa,
      fdimg: img
    })
    let {
      type
    } = e.currentTarget.dataset, va = '';
    if (type == 's2') {
      va = "s2";
    }
    this.setData({
      [va]: true
    })
  },
  confirmbtn(e) {
    console.log(e)
    console.log('点击了从新上传！')
    var aaa = this.data.fdaaa
    var that = this;
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        //营业执照
        if (aaa == 5) {
          that.yjsbyyz(data.imgUrl);
          that.setData({
            yyzz: urls,
            wuyyzz: data.imgUrl
          })
        }
        // 身份证
        if (aaa == 6) {
          that.setData({
            otherzz: urls,
            wuotherzz: data.imgUrl
          })
        }
        // 店头照
        if (aaa == 7) {
          that.setData({
            diantz: urls,
            wudiantz: data.imgUrl
          })
        }
        // 免冠照
        if (aaa == 8) {
          that.setData({
            miangz: urls,
            wumiangz: data.imgUrl
          })
        }
        // 店铺logo
        if (aaa == 9) {
          that.setData({
            dianplogo: urls,
            wudianplogo: data.imgUrl
          })
        }
      })
    })

  },
  cancelBtn() {
    console.log('点击了放大！')
    wx.previewImage({
      urls: [this.data.fdimg]
    })
  },
  getIptValue(e) {
    console.log(e.detail)
  },
  // 供应商验证
  gysyzs() {
    if (this.data.yyzz == '') {
      wx.showToast({
        title: '请上传营业执照',
        icon: 'none'
      })
    } else if (this.data.otherzz == '') {
      wx.showToast({
        title: '请上传其他证照',
        icon: 'none'
      })
    } else if (this.data.diantz == '') {
      wx.showToast({
        title: '请上传店头照',
        icon: 'none'
      })
    } else if (this.data.dianname == '') {
      wx.showToast({
        title: '请输入供应商名称',
        icon: 'none'
      })
    } else if (this.data.xinyong == '') {
      wx.showToast({
        title: '请输入社会代码',
        icon: 'none'
      })
    } else if (this.data.xinyong.length != 18) {
      wx.showToast({
        title: '请输入18位的社会代码',
        icon: 'none'
      })
    } else if (this.data.fapename == '') {
      wx.showToast({
        title: '请输入法人/负责人',
        icon: 'none'
      })
    } else if (this.data.xiangadress == '') {
      wx.showToast({
        title: '请输入经营地址',
        icon: 'none'
      })
    } else if (this.data.jydianhua == '') {
      wx.showToast({
        title: '请输入联系电话',
        icon: 'none'
      })
    } else if (this.data.jydianhua != '') {
      if (reg.test(this.data.jydianhua) == false) {
        wx.showToast({
          title: '请输入正确的联系电话',
          icon: 'none'
        })
      } else {
        this.tjgys();
      }
    } else {
      this.tjgys();
    }
  },
  // 接口
  tjgys() {
    var that = this
    that.setData({
      buttonClicked: true
    })
    this.myRequest(yuming + 'shop/ShopAddSupplier', {
      addid: that.data.addid,
      shopId: wx.getStorageSync('shopId'),
      supplierName: that.data.dianname,
      creditCode: that.data.xinyong,
      legalPerson: that.data.fapename,
      businessDress: that.data.xiangadress,
      supplierMobile: that.data.jydianhua,
      contactPhone: that.data.wuyyzz,
      thatPhoto: that.data.wuotherzz,
      doorPhoto: that.data.wudiantz,
      // bareheadedPhoto: that.data.wumiangz
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(function () {
          wx.navigateBack({
            delta: 0,
          })
        }, 1000)
      } else {
        that.setData({
          buttonClicked: false
          
        })
      }
    })
  },
  // 差掉
  chadiao(e) {
    var index = e.currentTarget.dataset.index
    var photo = this.data.photo;
    var wuphoto = this.data.wuphoto;
    var dd = wuphoto.split(",")
    photo.splice(index, 1)
    dd.splice(index, 1)
    var rr = dd.join(",")
    this.setData({
      photo: photo,
      wuphoto: rr
    })
  },
  // 多组放大
  fangda(e) {
    var img = e.currentTarget.dataset.img
    var list = e.currentTarget.dataset.list
    wx.previewImage({
      current: img,
      urls: list
    })
  },
  // 原料上传
  shangchuan(e) {
    var that = this
    var pics = [];
    var wuphoto = this.data.wuphoto;
    this.myChooseImage(9).then(res => {
      const tempFilePaths = res.tempFilePaths
      for (var i = 0; i < tempFilePaths.length; i++) {
        pics.push(tempFilePaths[i])
      }
      if ((pics.length) + (that.data.photo.length) > 9) {
        wx.showToast({
          title: '最多可上传' + 9 + "张",
          icon: 'none'
        })
      } else {
        for (var i = 0; i < tempFilePaths.length; i++) {
          (function (i) { // 注意这里是形参
            setTimeout(function () {
              that.duoupimg(tempFilePaths[i]).then(res => {
                wx.showLoading({
                  title: '上传图片中...',
                });
                var data = JSON.parse(res.data)
                var urls = data.httpUrl + data.imgUrl
                wuphoto = wuphoto.concat(data.imgUrl + ",")
                that.setData({
                  photo: that.data.photo.concat(urls),
                  wuphoto: wuphoto
                });
                if (i == tempFilePaths.length - 1) {
                  wx.hideLoading();
                }
              })
            }, 800 * i); // 还是每秒执行一次，不是累加的
          })(i) // 注意这里是实参，这里把要用的参数传进去
        }
      }
    })
  },
  // 上传
  chuan(e) {
    var aaa = e.currentTarget.dataset.aaa //店头营业执照
    var that = this;
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        //营业执照
        if (aaa == 5) {
          that.yjsbyyz(data.imgUrl);
          that.setData({
            yyzz: urls,
            wuyyzz: data.imgUrl
          })
        }
        // 身份证
        if (aaa == 6) {
          that.setData({
            otherzz: urls,
            wuotherzz: data.imgUrl
          })
        }
        // 店头照
        if (aaa == 7) {
          that.setData({
            diantz: urls,
            wudiantz: data.imgUrl
          })
        }
        // 免冠照
        if (aaa == 8) {
          that.setData({
            miangz: urls,
            wumiangz: data.imgUrl
          })
        }
        // 店铺logo
        if (aaa == 9) {
          that.setData({
            dianplogo: urls,
            wudianplogo: data.imgUrl
          })
        }
      })
    })
  },
  // 供应商
  dianname(e) {
    this.setData({
      dianname: e.detail.value
    })
  },
  // 经营类型
  bindjylx(e) {
    this.setData({
      jylxguo: this.data.jylxlist[e.detail.value].cartTitle,
      jylxid: this.data.jylxlist[e.detail.value].id
    })
  },
  // 信用代码
  xinyong(e) {
    const zhengze = /^[a-zA-Z0-9]+$/
    console.log(zhengze.test(e.detail.value));
    if(zhengze.test(e.detail.value) == true) {
      this.setData({
        xinyong: e.detail.value
      })
    }else{
      wx.showToast({
        title: '只能输入数字和字母',
        icon:'none'
      })
      this.setData({
        xinyong: ''
      })
    }


    // this.setData({
    //   xinyong: e.detail.value
    // })
  },
  // 法人
  fapename(e) {
    this.setData({
      fapename: e.detail.value
    })
  },
  // 经营地址
  xiangadress(e) {
    this.setData({
      xiangadress: e.detail.value
    })
  },
  // 联系电话
  jydianhua(e) {
    this.setData({
      jydianhua: e.detail.value
    })
  },
  // 添加
  tianjia() {
    var arr = {
      materialName: '',
      materialNums: '',
      materialUnit: ''
    }
    this.setData({
      lie: this.data.lie.concat(arr)
    })
  },
  // 生成二维码
  sheng() {
    wx.navigateTo({
      url: '/my_other/xinxilr/xinxilr',
    })
  },
  // 减
  jian() {
    console.log(1)
    this.setData({
      shul: this.data.shul -= 1
    })
  },
  // 加
  jia() {
    console.log(1)
    this.setData({
      shul: this.data.shul += 1
    })
  },
  // 选择开始时间
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  // 选择结束时间
  bindDateChanges: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      dates: e.detail.value
    })
  },
  // 商品名称
  bindspmc(e) {
    let index = e.detail.value
    this.setData({
      spmcindex: index,
    });
  },
  // 进货数量
  jinhuonum(e) {
    let index = e.detail.value
    this.setData({
      jinhuonumindex: index,
    });
  },
  // 数量单位
  numdan(e) {
    var index = e.currentTarget.dataset.index
    var item = this.data.lie
    item[index].materialUnit = this.data.numdanlist[index].name
    this.setData({
      sldwguo: item[index].materialUnit,
      lie: this.data.lie
    });
  },
  // 获取供应商
  gyslb() {
    var that = this
    this.myRequest(yuming + 'shop/ShopSelectSupplier', {
      shopId: wx.getStorageSync('shopId'),
      page: '-1'
    }, "POST").then(res => {
      that.setData({
        fazlist: res.data.data
      })
    })
  },
  // 供应商
  fazheng(e) {
    let index = e.detail.value
    this.setData({
      gongguo: this.data.fazlist[index].legalPerson,
      gongid: this.data.fazlist[index].id,
    });
  },
  // 原料类型
  xujylb(e) {
    let index = e.detail.value
    this.setData({
      yltypeguo: this.data.xukejylblist[index].name,
    });
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  // 经营类型查询
  jylx() {
    var that = this
    var typeMold = ''
    var shopRes = wx.getStorageSync('shopRes')
    if (shopRes == 0) { //0个体 1企业
      typeMold = 2 //1企业 2个体户
    }
    if (shopRes == 1) {
      typeMold = 1
    }
    this.myRequest(yuming + 'Besall/cartrelationList', {
      typeMold: typeMold
    }, "POST").then(res => {
      that.setData({
        jylxlist: res.data.data
      })
    })
  },
  // 获取商品名称
  spmcs(e) {
    var index = e.currentTarget.dataset.index
    var item = this.data.lie
    var value = e.detail.value
    item[index].materialName = value
    this.setData({
      lie: this.data.lie
    })
  },
  // 获取商品数量
  spsls(e) {
    var index = e.currentTarget.dataset.index
    var item = this.data.lie
    var value = e.detail.value
    item[index].materialNums = value
    this.setData({
      lie: this.data.lie
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      addid: options.addid,
      titileAdd:options.addtitle
    })
    if(wx.getStorageSync('scene') == '1047'){
      this.setData({
      addid:'11'
      })
    }

    this.jylx()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})